package cn.lz;

import cn.lz.pojo.PowerInfo;
import cn.lz.pojo.tang;
import com.alibaba.fastjson.JSON;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 暂时没用
 */
public class MsgHanler extends IoHandlerAdapter {
    private static final Logger log = LoggerFactory.getLogger(MsgHanler.class);

    @Override
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        // 出现异常
        log.error("--------exception--------");
        super.exceptionCaught(session, cause);
    }

    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        // 从服务器中接收到消息后的处理
        log.info("--------msg receive--------");
        log.info("Message:{}" + message.toString());
        super.messageReceived(session, message);
    }

    public static void main(String[] args) {
        tang t =new tang();
        t.setSixLen("000164");
        t.setCD("Z");
        t.setOD("5896348_1 ");
        t.setTY("TY");
        t.setID("5896348");
        t.setPW("895A4");
        t.setCC("13800138000");
        t.setTM("15922935317");
        t.setSL(5);
        t.setLO("116.341603,40.012486");
        t.setVR("1001");
        t.setSG("27,0");

        List<PowerInfo> longs = new ArrayList<PowerInfo>();
        PowerInfo p=new PowerInfo();
        p.setB1("1");
        longs.add(p);
        t.setLT(longs);
        String actual = JSON.toJSONString(t);

        System.out.println(actual);


//        String s = JSONObject.toJSONString(t);
//
//        System.out.println("数据是：" + s );
//
//        System.out.println("数据长度是:" +("000000"+ s.length()));
//        int youNumber = 1;
// // 0 代表前面补充0
// // 4 代表长度为4
//  // d 代表参数为正数型
//  String str = String.format("%06d", s.length());
//  System.out.println(DateUtils.UnixDate(new Date())); // 0001
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        // 往服务器中发送消息
        log.info("--------msg sent--------");

//        super.messageSent(session, message);

        tang t =new tang();
        t.setSixLen("000164");
        t.setCD("Z");
        t.setOD("5896348_1 ");
        t.setTY("TY");
        t.setID("5896348");
        t.setPW("895A4");
        t.setCC("13800138000");
        t.setTM("15922935317");
        t.setSL(5);
        t.setLO("116.341603,40.012486");
        t.setVR("1001");
        t.setSG("27,0");


        super.messageSent(session, "");
    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        // 当session被创建的时候调用
        log.info("--------session create--------");
        super.sessionCreated(session);
    }
}